package net.anzix.jhacks.flexclipse.properties;

import java.io.IOException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

public class FlexclipsePropertyPage extends PropertyPage {

	private static final String EMPTYSTRING = "";
	private static final String MAINMXML_TITLE = "&Main MXML:";
	private static final String CSS_TITLE = "&CSS:";
	private static final String OUTPUT_TITLE = "&Output:";
	private static final int TEXT_FIELD_WIDTH = 50;

	private Text mainMxmlText;
	private Text cssText;
	private Text outputText;

	/**
	 * Constructor for SamplePropertyPage.
	 */
	public FlexclipsePropertyPage() {
		super();
	}

	private void addMainMxmlSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);

		// Label for owner field
		Label mainMxmlLabel = new Label(composite, SWT.NONE);
		mainMxmlLabel.setText(MAINMXML_TITLE);

		// Owner text field
		mainMxmlText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		mainMxmlText.setLayoutData(gd);

		// Label for output field
		Label cssLabel = new Label(composite, SWT.NONE);
		cssLabel.setText(CSS_TITLE);

		// output text field
		cssText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData cssOutput = new GridData();
		cssOutput.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		cssText.setLayoutData(cssOutput);

		// Label for output field
		Label outputLabel = new Label(composite, SWT.NONE);
		outputLabel.setText(OUTPUT_TITLE);

		// output text field
		outputText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gdOutput = new GridData();
		gdOutput.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		outputText.setLayoutData(gdOutput);

		final ProjectPropertyLoader projectPropertyLoader = new ProjectPropertyLoader((IProject) getElement());
		
		// Populate owner text field
		mainMxmlText.setText(projectPropertyLoader.getMainMxml());
		final String defaultCSSOpt = projectPropertyLoader.getDefaultCSS();
		cssText.setText(defaultCSSOpt == null ? EMPTYSTRING : defaultCSSOpt);
		outputText.setText(projectPropertyLoader.getOutput());

	}

	/**
	 * @see PreferencePage#createContents(Composite)
	 */
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		addMainMxmlSection(composite);
		return composite;
	}

	private Composite createDefaultComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		return composite;
	}

	protected void performDefaults() {
		// Populate the owner text field with the default value
		mainMxmlText.setText(ProjectPropertyLoader.DEFAULT_MAINMXML);
	}

	public boolean performOk() {
		// store the value in the owner text field
		try {
			final ProjectPropertyLoader projectPropertyLoader = new ProjectPropertyLoader((IProject) getElement());
			projectPropertyLoader.setMainMxml(mainMxmlText.getText());
			final String cssOpt = cssText.getText();
			projectPropertyLoader.setDefaultCSS(EMPTYSTRING.equals(cssOpt) ? null : cssOpt );
			projectPropertyLoader.setOutput(outputText.getText());
			projectPropertyLoader.persistProperties(null);
		} catch (CoreException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
	}

}